/**
 * (c) Copyright Mirasol Op'nWorks Inc. 2002, 2003. 
 * http://www.opnworks.com
 * Created on Apr 2, 2003 by lgauthier@opnworks.com
 */

package com.free.javadodo.swt.ui;

import java.net.URL;

import javax.swing.ImageIcon;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITableLabelProvider;

import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

import com.free.javadodo.util.Constant;
import com.free.javaplus.model.ColumnModel;

/**
 * Label provider for the TableViewerExample
 * 
 * @see org.eclipse.jface.viewers.LabelProvider
 */
public class ColumnLabelProvider extends LabelProvider implements
		ITableLabelProvider {
	DoDoConfig dodo;
	
	public ColumnLabelProvider(DoDoConfig dodo_)
	{
		dodo = dodo_;
	}
	
	// Names of images used to represent checkboxes
	public static final String CHECKED_IMAGE = "checked";
	public static final String UNCHECKED_IMAGE = "unchecked";

	// For the checkbox images
	private static ImageRegistry imageRegistry = new ImageRegistry();

	/**
	 * Note: An image registry owns all of the image objects registered with it,
	 * and automatically disposes of them the SWT Display is disposed.
	 */

	/**
	 * Returns the image with the given key, or <code>null</code> if not
	 * found.
	 */
	private Image getImage(boolean isSelected) {
		String key = isSelected ? CHECKED_IMAGE : UNCHECKED_IMAGE;
		return imageRegistry.get(key);
	}

	public Image getImgLocal(boolean isSelected) {
		Image bugIcon = null;
		//URL url = ColumnLabelProvider.class.getResource("");
		String classpath ="";//url.toString().substring(6);
		//System.out.println("getImgLocal.classpat:"+classpath); 
		//System.out.println("getImgLocal.Constant.getCurrentWorkingDirectory():"+Constant.getCurrentWorkingDirectory());
		classpath = Constant.getCurrentWorkingDirectory()+"resources/other/";
		if (isSelected)
			bugIcon = new Image(dodo.sShell.getDisplay(),
					classpath + "checked.gif");
		else
			bugIcon = new Image(dodo.sShell.getDisplay(),
					classpath + "unchecked.gif");
		return bugIcon;
	}

	/**
	 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object,
	 *      int)
	 */
	public String getColumnText(Object element, int columnIndex) {
		String result = "";
		ColumnModel col = (ColumnModel) element;
		switch (columnIndex) {
		case 0: // COMPLETED_COLUMN
			result = col.getName();
			break; 
		case 1:
			result = col.getShowName();
			break;		 
		case 3:
			result = col.getAssigntype();
			break;
		case 5:
			result = col.getSearchKey();
			break;
		default:
			break;
		}
		return result;
	}

	/**
	 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object,
	 *      int)
	 */
	public Image getColumnImage(Object element, int columnIndex) {

		if (columnIndex == 2)   // COMPLETED_COLUMN?
		return getImgLocal(((ColumnModel) element).isCanNull());
		if (columnIndex == 4)   // COMPLETED_COLUMN?
			return getImgLocal(((ColumnModel) element).isShowInList());
		return null;
		
	}

}
